attribute vec3 aPointCoordinate;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;

uniform float xMin;
uniform float xMax;
uniform float yMin;
uniform float yMax;
uniform float zMin;
uniform float zMax;

uniform float left;
uniform float right;
uniform float top;
uniform float bottom;
uniform float front;
uniform float back;

uniform vec4 clickRgba;
uniform bool click;

varying float vValue;
varying float vMin;
varying float vMax;

varying vec4 vClickRgba;
varying float vClick;

float rescale(float v, float min, float max, float newMin, float newMax){
    if(max-min<0.00001){
        max=max+0.00001;
    }
    return newMin + (newMax-newMin)*v/(max-min);
}

vec3 getPositionFromCoordinate(vec3 coordinate){
    float x=rescale(coordinate.x, xMin, xMax, left, right);
    float y=rescale(coordinate.z, zMin, zMax, bottom, top);
    float z=rescale(coordinate.y, yMin, yMax, front, back);
    return vec3(x,y,z);
}

void main(void) {
    vec3 glPos=getPositionFromCoordinate(aPointCoordinate);
    gl_Position = uPMatrix * uMVMatrix * vec4(glPos, 1.0);
    vValue = aPointCoordinate.z;
    vMin=zMin;
    vMax=zMax;
    vClickRgba=clickRgba;
    vClick=click?1.0:-1.0;
}